import { createStore, Store, useStore as userVuexStore } from 'vuex'
import { IRootState, IStoreType } from './types'
import login from './login/login'
import system from './main/system/system'
import dashboard from './analysis/dashboard'
import { getPageListData } from '@/service/main/system/system'

const store = createStore<IRootState>({
  state() {
    return {
      name: 'shiyi',
      age: 18,
      entireDepartment: [],
      entireRole: [],
      entireMenu: []
    }
  },
  mutations: {
    changeEntireDepartment(state, list) {
      state.entireDepartment = list
    },
    changeEntireRole(state, list) {
      state.entireRole = list
    },
    changeEntireMenu(state, list) {
      state.entireMenu = list
    }
  },
  getters: {},
  actions: {
    async getInitDataAction({ commit }) {
      //请求完整部门和角色数据
      const DepartmentResult = await getPageListData('/department/list', {
        offset: 0,
        size: 1000
      })
      const { list: entireDepartment } = DepartmentResult.data

      const RoleResult = await getPageListData('/role/list', {
        offset: 0,
        size: 1000
      })
      const { list: entireRole } = RoleResult.data
      //保存数据
      commit('changeEntireDepartment', entireDepartment)
      commit('changeEntireRole', entireRole)

      //请求完整菜单数据
      const menuResult = await getPageListData('/menu/list', {})
      const { list: menulist } = menuResult.data
      commit('changeEntireMenu', menulist)
    }
  },
  modules: {
    login,
    system,
    dashboard
  }
})

export function setupStore() {
  store.dispatch('login/loadLocalLogin')
}
export function useStore(): Store<IStoreType> {
  return userVuexStore()
}
export default store
